//
//  TrainingDetailSubDataCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/12/1.
//

import UIKit
import MYModule

open class TrainingDetailSubDataCell: TrainingDetailBaseCell {
    
    open var totalData: TrainingDetailWorkoutsRecordModel? {
        
        didSet {
            

            guard let subDatas = totalData?.subDatas else { return }
            
            if subDatas.isEmpty {
                return
            }
            
            var count: CGFloat = 2
            if subDatas.count%3 == 0 {
                count = 3
            }
            
            layout.itemSize = CGSize(width: self.frame.width/count, height: 38)
            collectionView.collectionViewLayout = layout
            collectionView.reloadData()
            
            
            let height = subDatas.count > 3 ? 134 : 95
           
            bgView.snp.updateConstraints { make in
                make.height.equalTo(height)
            }
            
        }
        
    }
    
    

    public lazy var collectionView: CollectionView = {
        
        let collectionView = CollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.automaticallyContentSize = true
        collectionView.isScrollEnabled = false
        return collectionView

    }()
    
    public lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 20
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = .zero
        return layout
    }()
    
    open override func makeUI() {
        super.makeUI()
        
        collectionView.register(TrainingSubDataCell.self, forCellWithReuseIdentifier: NSStringFromClass(TrainingSubDataCell.self))
        collectionView.dataSource = self
        
        let topBgView = View()
        topBgView.backgroundColor = bgView.backgroundColor
        
        bgView.addSubview(topBgView)
        bgView.addSubview(collectionView)
        
        topBgView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(30)
        }
        
        bgView.snp.remakeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
            make.height.equalTo(134)
        }
        
        collectionView.snp.makeConstraints { make in
            
            make.left.right.equalToSuperview()
            make.centerY.equalToSuperview()
           
        }
        
    }

}

extension TrainingDetailSubDataCell: UICollectionViewDataSource {
    
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return totalData?.subDatas.count ?? 0
    }
    
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(TrainingSubDataCell.self), for: indexPath) as! TrainingSubDataCell
        
        cell.subData = totalData?.subDatas[indexPath.item]
        
        return cell
        
    }
    
}

extension TrainingDetailSubDataCell {
    
    open class TrainingSubDataCell: CollectionViewCell {
        
        open var subData: TrainingDetailTotalDataSubModel? {
            
            didSet {
                
                guard let subData = subData else {
                    return
                }

                titleLabel.text = subData.title
                dataView.icon.image = UIImage(named: subData.iconName)
                dataView.numberLabel.text = subData.value
                dataView.unitLabel.text = subData.unit
            }
        }
        
        public let dataView = TotalDataView()

        public let titleLabel = Label()
        
        open override func makeUI() {
            super.makeUI()
            
            titleLabel.font = Fonts.systemFont(ofSize: 12.0)
            titleLabel.textColor = UIColor.assist_D0D0D0
            
            addSubview(dataView)
            addSubview(titleLabel)
            
            dataView.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview()
                make.right.lessThanOrEqualToSuperview()
            }
            
            titleLabel.snp.makeConstraints { make in
                make.top.equalTo(dataView.snp.bottom).offset(3)
                make.centerX.equalToSuperview()
                make.right.lessThanOrEqualToSuperview()
                make.bottom.equalToSuperview()
            }
        }
        
    }
    
}

extension TrainingDetailSubDataCell {
    
    open class TotalDataView: View {
        
        public let icon = ImageView()
        public let numberLabel = Label()
        public let unitLabel = Label()
        
        open override func makeUI() {
            super.makeUI()
            
            numberLabel.font = Fonts.boldSystemFont(ofSize: 16.0)
            numberLabel.textColor = UIColor.assist_222222
            
            unitLabel.font = Fonts.systemFont(ofSize: 10)
            unitLabel.textColor = UIColor.assist_222222
            
            
            addSubview(icon)
            addSubview(numberLabel)
            addSubview(unitLabel)
            
            icon.snp.makeConstraints { make in
                make.left.equalToSuperview()
                make.centerY.equalToSuperview()
                make.bottom.lessThanOrEqualToSuperview()
            }
            
            numberLabel.snp.makeConstraints { make in
                make.left.equalTo(icon.snp.right).offset(5)
                make.centerY.equalToSuperview()
                make.bottom.lessThanOrEqualToSuperview()
            }
            
            unitLabel.snp.makeConstraints { make in
                make.left.equalTo(numberLabel.snp.right).offset(2)
                make.centerY.equalToSuperview()
                make.right.equalToSuperview()
                make.bottom.lessThanOrEqualToSuperview()
            }
        }
    }
    
    
}
